package com.kotlin.flong.coroutines

import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import java.lang.Exception

/**
 * @Author:liangjl
 * @Date: 2020/7/7-20:41
 * @Eamil:jilongliang@sina.com
 * @Description:
 * 关于父子协程的异常和取消问题
 * 1、协程的取消总是沿着协程层次体系向上进行传播
 */
fun main() = runBlocking<Unit> {

    try {
        failureComputation()
    } finally {
        println("failureComputation")
    }
}

private suspend fun failureComputation(): Int = coroutineScope {
    var value1 = async<Int> {
        try {
            delay(900000)
            50
        } finally {
            println("finally")
        }
    }

    var value2 = async<Int> {
        Thread.sleep(2000)
        println("value2 Thread sleep")
        throw  Exception()
    }
    value1.await() + value2.await()

}